package com.cuit.jifver.weibo.data;

import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;

import com.cuit.jifver.weibo.model.HomeTimeLineRequest;
import com.cuit.jifver.weibo.model.SinaStatuses;
import com.cuit.jifver.weibo.model.TextMessage;
import com.cuit.jifver.weibo.model.WeiBoList;
import com.cuit.jifver.weibo.model.sina.Status;
import com.cuit.jifver.weibo.model.sina.Statuses;
import com.cuit.jifver.weibo.net.SyncHttp;
import com.cuit.jifver.weibo.utils.Constants;
import com.cuit.jifver.weibo.utils.LogUtil;
import com.cuit.jifver.weibo.utils.TextUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


public class SinaWeibo extends Weibo {

	@Override
	public String publishTextMessage(Context context, TextMessage message) {
		WeiboParameters parameters = new WeiboParameters();
		parameters.add("status", "test");
		String str = null;
		/*try {
			str = OAuth.getPostParams(Constants.sina.URL_PUBLISH_TEXT_MESSAGE, "POST", parameters,Weibo.APP_SECRET, weibo.getAccessToken().getSecret());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		SyncHttp http = new SyncHttp();
		try {
			str = http.httpPost(Constants.sina.URL_PUBLISH_TEXT_MESSAGE, str);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		return str;
	}

	@Override
	public List<Status> getHomeTimeLine(Context context, HomeTimeLineRequest request)
	{
		String result = "";
		List<Status> list = null;
		WeiboParameters params = new WeiboParameters();
		params.add("access_token", Weibo.getInstance().getAccessToken());
		//params.add("since_id", "test");
		//params.add("max_id", "test");
		params.add("count", "0");
		params.add("page", "1");
		params.add("base_app", "0");
		params.add("feature", "0");
		
		try
		{
			WeiboParameters parameters = OAuth.getHttParameters("get", Constants.sina.URL_HOME_TIME_LINE, params, Weibo.getInstance().getAccessToken(), Weibo.getInstance().getAccessSecret());
			SyncHttp http = new SyncHttp();
			result = http.httpGet(Constants.sina.URL_HOME_TIME_LINE, parameters);
			
			//result = result.subSequence(1, result.length() - 1).toString();
			LogUtil.i(this, "sina home timeline = "+result);
			TextUtil.witeFile("sinahome.json", result);
			Gson gson = new Gson();
			
			Type type = new TypeToken<LinkedList<Status>>(){}.getType();		 
			list = gson.fromJson(result, type);
			
			//TypeToken<T>
			//SinaStatuses statuses = gson.fromJson(result, SinaStatuses.class);
			LogUtil.i(this, "size = "+list.size());
			
		}
		catch (Throwable e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return list;
	}

}
